home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.20000114-20000217 / 000166_news@columbia.edu _Thu Jan 27 18:58:17 2000.msg < prev    next >
Internet Message Format  |  2020-01-01  |  8KB

  1. Return-Path: <news@columbia.edu>
  2. Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
  3.     by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id SAA28379
  4.     for <kermit.misc@watsun.cc.columbia.edu>; Thu, 27 Jan 2000 18:58:16 -0500 (EST)
  5. Received: (from news@localhost)
  6.     by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id SAA22600
  7.     for kermit.misc@watsun.cc.columbia.edu; Thu, 27 Jan 2000 18:52:08 -0500 (EST)
  8. X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
  9. From: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  10. Subject: Case Study #17: Fun with Dates and Times
  11. Date: 27 Jan 2000 23:52:05 GMT
  12. Organization: Columbia University
  13. Message-ID: <86qln5$m25$1@newsmaster.cc.columbia.edu>
  14. To: kermit.misc@columbia.edu
  15.  
  16.  
  17. C-Kermit 7.0 (and Kermit 95 1.1.18 when it comes out) let you use dates 
  18. and times as "native" data types in your commands and scripts.  We spoke
  19. about this a little bit in the "How Do I Delete Files More Than a Week
  20. Old?" example but that just skimmed the surface.
  21.  
  22. I won't bother listing the rules for writing dates and times; they are
  23. spelled out in Section 1.6 of ckermit2.txt.  Assuming you know how to
  24. write dates and times, what can you do with them?
  25.  
  26. You have already seen how they can be used as file selectors.  For example,
  27. to send all files that were created in December 1999, you could:
  28.  
  29.   SEND /NOT-BEFORE:1999-12-01 /BEFORE:2000-01-01 *.*
  30.  
  31. /NOT-BEFORE is almost the same as /AFTER, except /NOT-BEFORE includes the
  32. given date-time (in this case the implied time of 00:00:00 on December 1),
  33. whereas /AFTER excludes it.  A fine point indeed!  (Of course there is also
  34. a /NOT-AFTER switch, which has the same relationship to /BEFORE.)
  35.  
  36. To illustrate dates with explicit times, here is a command to send log files
  37. created after noon on the 27th day of January of this year:
  38.  
  39.   SEND /AFTER:{2000-01-27 12:00:00} *.log
  40.  
  41. A date-time is a single field (or "word") in Kermit syntax, so must be
  42. enclosed in braces if it contains any spaces, as shown above (of course
  43. other variations are possible).
  44.  
  45. To experiment with date-time formats, use C-Kermit's new DATE command.  If
  46. you type "date" by itself, it prints the current date and time.  If you
  47. follow it by something, C-Kermit tries to interpret the "something" as a
  48. date and/or time and prints the result (or an error message).
  49.  
  50. Perhaps the most interesting aspect of C-Kermit's new date-time support is
  51. the ability to do date and time arithmetic.  This works in two ways.  First,
  52. by using the built-in relative date syntax: YESTERDAY, TOMORROW, +3DAYS,
  53. -6WEEKS, +12YEARS, etc, for example:
  54.  
  55.   DELETE /BEFORE:-2WEEKS *.*
  56.  
  57. All the built-in symbolic dates are relative to "today", the current date.
  58.  
  59. You can also perform explicit calculations, such as obtaining a date that is
  60. a given number of days after a given date, or finding out the day of the
  61. week for a given date.
  62.  
  63. All of C-Kermit's date calculations are based on the Modified Julian Date
  64. (MJD), which is the number of days since 17 November 1858.  For dates prior
  65. to 17 Nov 1858, the MJD is negative.  MJDs are scalar counting numbers
  66. (unlike the "yyyyddd" format, often but improperly called the Julian date),
  67. so you can do arithmetic on them.  For example, if today's MJD is 51570,
  68. then 100 days from today, the MJD will be 51570 + 100 = 51670.
  69.  
  70. MJDs have some other interesting properties too:
  71.  
  72.  . They can be compared arithmetically.  This lets you (for example) 
  73.    select files that are older (or newer) than some other file, or use
  74.    the MJD as a numeric sort key.
  75.  
  76.  . The modulus of the MJD and 7 tells the day of the week (4=Sunday,
  77.    5=Monday, 6=Tuesday, 0=Wednesday, ...)
  78.  
  79. C-Kermit 7.0 includes functions to convert date-time strings to MJD and
  80. vice versa.  For example, here's how to get the date 100 days from today:
  81.  
  82.   .\%m := \fmjd(\v(date))     ; Convert today's date to MJD
  83.   echo \fmjd2date(\%m+100)    ; Add 100 and convert back to a date.
  84.  
  85. The result is "20000506", or 6 May 2000, which is:
  86.  
  87.   echo \fday(20000506)
  88.   Sat
  89.  
  90. ... a Saturday.  Did you ever wonder what day of the week you were born?
  91. Ask Kermit (substituting your actual birthday):
  92.  
  93.   echo \fday(27 jan 1958)
  94.   Mon
  95.  
  96. The \fday() function returns the English 3-letter day of the week
  97. abbreviation; \fnday() returns a day-of-week number, 0-6; the argument is
  98. a date-time string.  You can use \fmodulus(MJD,7) if you have an MJD.
  99.  
  100. By the way, the ".<variablename> = <value>" notation is new to C-Kermit 7.0.
  101. The assignment operator can be "=", which does what DEFINE does; ":=" does
  102. what ASSIGN does, and "::=" evaluates the <value> as an arithmetic
  103. expression and then ASSIGNs it to the <variable> (see Section 7.9.1 of the
  104. ckermit2.txt file for details).
  105.  
  106. C-Kermit's new date conversion and arithmetic functions let us answer
  107. questions like "What is the first Friday after a given date?".  Questions
  108. like this come up all the time in data processing.  Let's say the variable
  109. \%d contains the "given date":
  110.  
  111.   .\%m ::= \fmjd(\%d)+1       ; Convert day after given date to MJD.
  112.   .\%x := \fmod(\%m,7)        ; Day of week of day after given date.
  113.   .\%y := \fmod(9-\%x,7)      ; Days until Friday (0-6)
  114.   .\%e := \fmjd2date(\%m+\%y) ; Date of first Friday after given date.
  115.  
  116. C-Kermit doesn't have a built-in function that returns the date of the first
  117. Friday after a given date, but (as explained in "Using C-Kermit", 2nd Ed,
  118. page 399), you can define your own:
  119.  
  120.   define FRIDAYAFTER {
  121.       local \%m \%x \%y
  122.       .\%m ::= \fmjd(\%*)+1   ; "\%*" allows for spaces in argument
  123.       .\%x := \fmod(\%m,7)    ; (see ckermit2.txt Section 7.5)
  124.       .\%y := \fmod(9-\%x,7)
  125.       return \fmjd2date(\%m+\%y)
  126.   }
  127.  
  128. You can invoke it like this:
  129.  
  130.   \fexec(fridayafter,27 jan 2000)
  131.  
  132. And you can use it for file selection like this:
  133.  
  134.   delete /before:\fexec(fridayafter,27 jan 1999)
  135.  
  136. C-Kermit also has another set of functions for dealing with the more common
  137. (but less useful) yyyyddd (year, day of year) format: \fdoy(date-time),
  138. fdoy2date(doy).  You can read about them in Section 1.6 of ckermit2.txt.
  139.  
  140. Arithmetic can also be done with times-of-day.  Functions are available to
  141. give the current time in hh:mm:ss notation and as seconds since midnight.
  142. New functions \fntime(hh:mm:ss) converts the given time to seconds since
  143. midnight, and \fn2time(n) converts the given number of seconds since
  144. midnight to hh:mm:ss format.  So given a particular time, you can convert it
  145. to seconds-since-midnight, do any desired arithmetic on it, and then convert
  146. the result back to hh:mm:ss format.
  147.  
  148. To illustrate, let's see What the date and time will be a million seconds
  149. from now:
  150.  
  151.   .\%t := \v(ntime)                  ; Current time secs since midnight
  152.   .\%d := \fmjd(\v(date))            ; MJD of today
  153.   incr \%t 1000000                   ; Add a million seconds to current time
  154.   incr \%d (\%t/86400)               ; Extract days and add to MJD
  155.   .\%t := \fmod(\%t,86400)           ; Remainder is time of day
  156.   echo \fmjd2date(\%d) \fn2time(\%t) ; Print the result
  157.  
  158. (86400 is the number of seconds in a day.)
  159.  
  160. C-Kermit's date/time functions don't use any platform-specific APIs (except
  161. obviously to obtain the current date and time), so they should work
  162. uniformly everywhere.  The range of date arithmetic depends on the integer
  163. size of your computer and the code generated by the C compiler.
  164.  
  165. And in case you were wondering, the true Julian date is the number of days
  166. since Noon, 1 January 4713 BC (BCE).  But this has become a rather large
  167. number (too large for 16- or even 18-bit computer words), so the MJD is used
  168. instead in most computer applications and is, in fact, the internal date
  169. representation in some well-known computer operating systems (but not Unix).
  170.  
  171. - Frank